package com.itheima.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2022/3/8 14:31
 * @description
 */
public class MyServer {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(8888);


        while (true){
            try {
                Socket socket = serverSocket.accept();
                InputStream inputStream = socket.getInputStream();

                byte[] buffer=new byte[8192];

                inputStream.read(buffer);


                String received = new String(buffer);
                String decode = URLDecoder.decode(received, "utf-8");
                System.out.println(decode);

                System.out.println("结束了~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");


                OutputStream outputStream = socket.getOutputStream();

                String back="<div>哥们我收到你的数据了!!!!</div>";
                int len=back.getBytes().length;

                String content= "HTTP/1.1 200 OK\r\n"+
                        "server: xiaohupaifuwuqi\r\n" +
                        "Content-Type: text/html;charset=utf-8\r\n" +
                        "Content-Length: "+len+"\r\n\r\n";

                content=content+back;


                outputStream.write(content.getBytes());

                outputStream.flush();
                inputStream.close();
                socket.close();

            }catch (Exception e){

                e.printStackTrace();
            }
        }
    }
}
